package com.lzm.ydpt.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.byt.framlib.commonutils.DisplayUtil;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.R;

/**
 * 公共对话框
 */
public class CommonTisDialog implements View.OnClickListener {

    private Dialog mDialog;
    private View dialogView;

    private TextView tv_common_tis_content;
    private TextView tv_common_tis_sure;
    private ImageView tv_common_tis_close;
    private Builder mBuilder;

    public CommonTisDialog(Builder builder) {
        this.mBuilder = builder;
        mDialog = new Dialog(mBuilder.getContext(), R.style.MyDialogStyle);
        dialogView = View.inflate(mBuilder.getContext(), R.layout.dialog_common_tis, null);

        tv_common_tis_content = dialogView.findViewById(R.id.tv_common_tis_content);
        tv_common_tis_sure = dialogView.findViewById(R.id.tv_common_tis_sure);
        tv_common_tis_close = dialogView.findViewById(R.id.tv_common_tis_close);

        mDialog.setContentView(dialogView); // 一定要在setAttributes(lp)之前才有效
        //设置dialog的宽
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DisplayUtil.getScreenWidth(mBuilder.getContext());
        lp.height = DisplayUtil.getScreenHeight(mBuilder.getContext());
        dialogWindow.setAttributes(lp);
        mDialog.setCanceledOnTouchOutside(builder.isTouchOutside());
        if (builder.getType() == 1) {
            tv_common_tis_content.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(builder.getContent())) {
                tv_common_tis_content.setText(builder.getContent());
            }
            tv_common_tis_sure.setText("立即充值");
        } else if (builder.getType() == 2) {
            tv_common_tis_content.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(builder.getContent())) {
                tv_common_tis_content.setText(builder.getContent());
            }
            tv_common_tis_sure.setText("查看报告");
        }
        tv_common_tis_sure.setOnClickListener(this);
        tv_common_tis_close.setOnClickListener(this);
    }

    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }

        switch (v.getId()) {
            case R.id.tv_common_tis_sure:
                dismiss();
                if (mBuilder.getOnListener() != null) {
                    mBuilder.getOnListener().onSure();
                }
                break;
            case R.id.tv_common_tis_close:
                dismiss();
                break;
        }
    }

    public static class Builder {

        private OnKnowListener onListener;
        private boolean isTouchOutside;
        private Context mContext;
        private String content;

        /**
         * 1 余额不足弹窗
         * 2 服务介绍弹窗
         */
        private int type = 1;

        public Builder(Context context) {
            this.mContext = context;
            this.onListener = null;
            this.isTouchOutside = true;
        }

        public OnKnowListener getOnListener() {
            return onListener;
        }

        public Builder setOnListener(OnKnowListener onListener) {
            this.onListener = onListener;
            return this;
        }

        public boolean isTouchOutside() {
            return isTouchOutside;
        }

        public Builder setCanceledOnTouchOutside(boolean isTouchOutside) {
            this.isTouchOutside = isTouchOutside;
            return this;
        }

        public CommonTisDialog build() {
            return new CommonTisDialog(this);
        }

        public Context getContext() {
            return mContext;
        }

        public Builder setContext(Context context) {
            mContext = context;
            return this;
        }

        public Builder setTouchOutside(boolean touchOutside) {
            isTouchOutside = touchOutside;
            return this;
        }

        public int getType() {
            return type;
        }

        public Builder setType(int type) {
            this.type = type;
            return this;
        }

        public String getContent() {
            return content;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }
    }

    public interface OnKnowListener {
        /**
         * 确定
         */
        void onSure();
    }
}